﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Ferpect.ComponentModel.Design
{
    [ToolboxItemFilter("Ferpect.ComponentModel.Screen", ToolboxItemFilterType.Require)]
    public class ScreenManagerDesigner : DocumentDesigner
    {
        private ScreenManagerControl screenManagerControl;
        private ScreenManager screenManager;
        private IDesignerHost host;
        private IComponentChangeService componentChangeService;

        private Point firstMouse;
        private Point lastMouse;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            this.screenManagerControl = (ScreenManagerControl)component;
            this.screenManager = this.screenManagerControl.Component;

            this.host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            this.componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
            if (this.componentChangeService != null)
            {
                this.componentChangeService.ComponentAdded += this.OnComponentAdded;
                this.componentChangeService.ComponentRemoved += this.OnComponentRemoved;
                this.componentChangeService.ComponentChanged += this.OnComponentChanged;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.componentChangeService.ComponentAdded -= this.OnComponentAdded;
                this.componentChangeService.ComponentRemoved -= this.OnComponentRemoved;
                this.componentChangeService.ComponentChanged -= this.OnComponentChanged;
            }
            base.Dispose(disposing);
        }

        protected override void PostFilterProperties(System.Collections.IDictionary properties)
        {
            base.PostFilterProperties(properties);
            if (properties.Contains("DoubleBuffered"))
            {
                properties.Remove("DoubleBuffered");
            }
        }

        private void OnComponentAdded(object sender, ComponentEventArgs args)
        {
            Screen screen = args.Component as Screen;

            if (args != null && !this.host.Loading)
            {
                if (screen != null && !this.screenManager.Screens.Contains(screen.Site.Name))
                {
                    this.screenManager.Screens.Add(screen);
                }
            }
        }

        private void OnComponentRemoved(object sender, ComponentEventArgs args)
        {
            if (args != null && !this.host.Loading)
            {
                Screen screen = args.Component as Screen;
                if (screen != null && this.screenManager.Screens.Contains(screen.Site.Name))
                {
                    this.screenManager.Screens.Remove(screen);
                }
            }
        }

        private void OnComponentChanged(object sender, ComponentChangedEventArgs args)
        {
        }

        protected override bool GetHitTest(System.Drawing.Point pt)
        {
            return false;
        }

        protected override void OnMouseDragBegin(int x, int y)
        {
            this.firstMouse = new Point(x, y);
            this.lastMouse = this.firstMouse;
        }

        protected override void OnMouseDragEnd(bool cancel)
        {
            Point pt = this.lastMouse;
            foreach (IComponent component in this.host.Container.Components)
            {
                ScreenComponentDesigner designer = this.host.GetDesigner(component) as ScreenComponentDesigner;
                if (designer != null && designer.GetHitTest(pt))
                {
                    ISelectionService selection = (ISelectionService)this.GetService(typeof(ISelectionService));
                    selection.SetSelectedComponents(new IComponent[] { component });
                }
            }
        }

        protected override void OnMouseDragMove(int x, int y)
        {
            this.lastMouse = new Point(x, y);
        }

        protected override void OnContextMenu(int x, int y)
        {
            Point pt = new Point(x, y);
            foreach (IComponent component in this.host.Container.Components)
            {
                ScreenComponentDesigner designer = this.host.GetDesigner(component) as ScreenComponentDesigner;
                if (designer != null && designer.GetHitTest(pt))
                {
                    ISelectionService selection = (ISelectionService)this.GetService(typeof(ISelectionService));
                    selection.SetSelectedComponents(new IComponent[] { component });
                    this.ShowChildContextMenu(pt);
                    return;
                }
            }
            base.OnContextMenu(x, y);
        }

        private void ShowChildContextMenu(System.Drawing.Point pt)
        {
            IMenuCommandService menuService = this.GetService(typeof(IMenuCommandService)) as IMenuCommandService;
            Guid wfMenuGroup = new Guid("{74D21312-2AEE-11d1-8BFB-00A0C90F26F7}");
            CommandID TraySelectionMenu = new CommandID(wfMenuGroup, 0x503);
            menuService.ShowContextMenu(TraySelectionMenu, pt.X, pt.Y);
        }
    }
}
